package com.reciver.common.config;

import java.util.Map;
import java.util.HashMap;
import javax.sql.DataSource;
import org.springframework.core.io.Resource;
import com.alibaba.druid.pool.DruidDataSource;
import com.reciver.common.enums.DataSourceType;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import com.reciver.common.datasource.DynamicDataSource;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

@Configuration 
@MapperScan("com.reciver.mapper")
public class MybatisConfig {

	@Primary
	@Bean(name="db1DataSource")
	@ConfigurationProperties(prefix="spring.datasource.db1")
	public DataSource db1DataSource(DruidProperties druidProperties) {
		DruidDataSource dataSource=DruidDataSourceBuilder.create().build();
		return druidProperties.dataSource(dataSource);
	}
	@Bean(name="db2DataSource")
	@ConfigurationProperties(prefix="spring.datasource.db2")
	public DataSource db2DataSource(DruidProperties druidProperties) {
		DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
		return druidProperties.dataSource(dataSource);
	}
	@Bean(name="db3DataSource")
	@ConfigurationProperties(prefix="spring.datasource.db3")
	public DataSource db3DataSource(DruidProperties druidProperties) {
		DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
		return druidProperties.dataSource(dataSource);
	}
	@Bean
    public DynamicDataSource dataSource(
    	   @Qualifier("db1DataSource")DataSource db1DataSource,
           @Qualifier("db2DataSource")DataSource db2DataSource,
           @Qualifier("db3DataSource")DataSource db3DataSource) {
        Map<Object, Object> map = new HashMap<>();
        // 添加的key为String类型
        map.put(DataSourceType.db1.getDb(), db1DataSource);
        map.put(DataSourceType.db2.getDb(), db2DataSource);
        map.put(DataSourceType.db3.getDb(), db3DataSource);
        // 通过单例获取对象
        DynamicDataSource dynamicDataSource = DynamicDataSource.getInstance();
        dynamicDataSource.setTargetDataSources(map);
        dynamicDataSource.setDefaultTargetDataSource(db1DataSource);
        return dynamicDataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DynamicDataSource dynamicDataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dynamicDataSource);
//        factoryBean.setTypeAliasesPackage();
        // 设置mapper.xml的位置路径
        Resource[] resources=new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml");
        factoryBean.setMapperLocations(resources);
        return factoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager(DynamicDataSource dynamicDataSource){
        return new DataSourceTransactionManager(dynamicDataSource);
    }
	
}